package com.cloudknow.utils;

import android.content.Context;
import android.widget.Toast;

public class ToastUtils {
    private static String oldMsg;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

    /**
     * 多次点击只显示一次
     */
    public static void showToast(Context context, String s) {
        if (StringUtils.isNull(s)){
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(context.getApplicationContext(), s, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    /**
     * 多次点击只显示一次
     */
    public static void showToast(Context context, String s, int isBottom) {
        if (StringUtils.isNull(s)){
            return;
        }
        if (toast == null) {
            toast.setGravity(isBottom, 0, 0);
            toast = Toast.makeText(context.getApplicationContext(), s, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    /**
     * 多次点击只显示一次
     */
    public static void showToast(Context context, int rId) {
        if (toast == null) {
            toast = Toast.makeText(context, context.getText(rId), Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (context.getText(rId).equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = context.getText(rId).toString();
                toast.setText(context.getText(rId));
                toast.show();
            }
        }
        oneTime = twoTime;
    }
}
